package com.david.concurrent.deadlock;

/**
 * 破坏死锁:使用资源申请的有序性原则就可破坏死锁
 * @author wade
 * @version 1.0
 * @date 2021/7/21 13:33
 */
public class BreakDeadLockTest {

    private final static Object RESOURCE_A=new Object();
    private final static Object RESOURCE_B=new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (RESOURCE_A){
                System.out.println(Thread.currentThread()+"get resourceA");
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread()+"waiting get resourceB");
                synchronized (RESOURCE_B){
                    System.out.println(Thread.currentThread()+"get resourceB");
                }
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (RESOURCE_A){
                System.out.println(Thread.currentThread()+"get resourceA");
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread()+"waiting get resourceB");
                synchronized (RESOURCE_B){
                    System.out.println(Thread.currentThread()+"get resourceB");
                }
            }
        });
        t1.start();
        t2.start();
    }
}
